package com.lagou.zookeeper.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

import java.util.List;
import java.util.concurrent.CountDownLatch;

public class Test {

    private  static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws Exception {

        RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);
        // 使用fluent编程风格
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("192.168.33.19:2181")
                .sessionTimeoutMs(50000)
                .connectionTimeoutMs(30000)
                .retryPolicy(exponentialBackoffRetry)
                .namespace("Configuration")  // 独立的命名空间 /DatabaseConfig
                .build();
        client.start();

        //创建节点
//        String context = "jdbc.driver=com.mysql.jdbc.Driver\n" +
//                "jdbc.url=jdbc:mysql://192.168.33.18:3306/test?characterEncoding=utf8\n" +
//                "jdbc.username=root\n" +
//                "jdbc.password=root";
//        client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/DatabaseConfig",context.getBytes());

        //更新节点
        String context2 = "jdbc.driver=com.mysql.jdbc.Driver\n" +
                "jdbc.url=jdbc:mysql://192.168.33.18:3306/test?characterEncoding=utf8\n" +
                "jdbc.username=root\n" +
                "jdbc.password=root";

        client.setData().forPath("/DatabaseConfig",context2.getBytes());
        countDownLatch.await();
    }

}
